#include "Hut.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>

Hut::Hut(void) : Filter(2, 1)
{
	image = cv::imread("hut.png", -1);
}


Hut::~Hut(void)
{
}

void Hut::Process()
{
	cv::Rect r;
	r.x = inputs[1]->at<int>(0, 0);
	r.y = inputs[1]->at<int>(0, 1);
	r.width = inputs[1]->at<int>(0, 2);
	r.height = inputs[1]->at<int>(0, 3);

	outputs[0] = inputs[0]->clone();

	//resize image
	double f = 451.0 / 640.0;

	cv::Mat imageResized;
	cv::resize(image, imageResized, cv::Size(r.width * 2, r.width * 2 * f));

	r.x -= r.width / 2;
	r.y -= 1.5 * r.height;
	r.width *= 2;
	r.height *= 2;

	for (int i = 0; i < imageResized.rows; ++i)
	{
		cv::Vec3b *p = outputs[0].ptr<cv::Vec3b>(i + r.y);
		cv::Vec4b *q = image.ptr<cv::Vec4b>(i);
		
		for (int j = 0; j < imageResized.cols; ++j)
		{
			p[j][0] = ((255.0 - q[j][3]) * p[j][0] + q[j][0] * q[j][3])/255.0;
			p[j][1] = ((255.0 - q[j][3]) * p[j][1] + q[j][1] * q[j][3])/255.0;
			p[j][2] = ((255.0 - q[j][3]) * p[j][2] + q[j][2] * q[j][3])/255.0;
		}
	}
}

void Hut::ProcessInvalid()
{
	outputs[0] = inputs[0]->clone();
}